const mysql = require('mysql');

const db_config = require('@/config/db');

var pool = mysql.createPool({
	protocol: db_config.protocol,
	host: db_config.host,
	database: db_config.database,
	port: db_config.port,
	user: db_config.user,
	password: db_config.password,
	query: { pool: true, debug: true }
});

let query = (sql, values) => {
	return new Promise((resolve, reject) => {
		pool.getConnection((err, connection) => {
			if (err) {
				reject(err)
			} else {
				connection.query(sql, values, (err, rows) => {
					if (err) {
						reject(err)
					} else {
						resolve(rows)
					}
					connection.release()
				})
			}
		})
	}).catch((err) => {
		return err
	})
}

module.exports = query
